import { userLogin } from "@/api/user";
import router from "@/router"; //此处的router，等同于this.$router
export default {
  namespaced: true,
  state() {
    return {
      userInfo: null,
      memo: "/home", //记录用户进入登录面板前，想要访问的路由
    };
  },
  mutations: {
    initUserInfoMut(state, info) {
      state.userInfo = info;
    },
    memoMut(state, path) {
      state.memo = path;
    },
  },
  actions: {
    userLoginAction(context, account) {
      // 1. 发起登录请求
      userLogin(account).then((res) => {
        console.log(res);
        // 2. 把用户信息存入state
        context.commit("initUserInfoMut", res.data);
        // 3. 触发路由跳转
        if (context.state.memo == "/mine") {
          router.push("/mine");
        } else {
          router.go(-1);
        }

        // router.push(context.state.memo); //路由访问历史记录会出问题
      });
    },
  },
};
